
%MACRO print_events(in);

*****************************************************************************
* Program: PRINT_EVENTS					   						
* 																			
* Description: Plots time-using and outcome-free-time. Plots KM curves.
*				Creates tables with relapse/non-relapse rates at selected
*				timepoints. Creates tables with quartile non-relapse times.
*						
* Last modified: August 11, 2015												
* Created by: Martin Wegman													
*****************************************************************************;

%let date = %sysfunc(date(),DATE7.);
ods noproctitle;
ODS ESCAPECHAR='^';

%let dat = data; * Dataset name stem;

%let v = 28; * From compute_events sas file;

data loc.&dat.&v;
	set loc.&in;
run;

****************************************************************************;
****************************************************************************;
**************      Plot Time-Using and Free-Time         ******************;
****************************************************************************;
****************************************************************************;

%MACRO print_free_time(v,type);

	%let list = opi amp bzd any mtd hiv;

	%do i = 1 %to 6;

		%let var = %scan(&list, &i);
		%put &var;

		%if &type=free %then %do;

			%if &var =opi %then %let Title  = 'Figure S5a. Boxplot of opiate-free time.';
			%else %if &var =amp %then %let Title  =  'Figure S5b. Boxplot of amphetamine-free time.';
			%else %if &var =bzd %then %let Title  = 'Figure S5c. Boxplot of benzodiazepine-free time.';
			%else %if &var =any %then %let Title  = 'Figure S5d. Boxplot of illicit-drug-free time.';
			%else %if &var =mtd %then %let Title  = 'Figure S5e. Boxplot of methadone-free time.';
			%else %if &var =bup %then %let Title  = 'Boxplot of buprenorphine-free time.';
			%else %if &var =hiv %then %let Title  =  'Figure S5f. Boxplot of HIV-free time';
		%end;
		%else %if &type=present %then %do;

			%if &var =opi %then %let Title  = 'Figure S6a. Boxplot of time using opiates.';
			%else %if &var =amp %then %let Title  =  'Figure S6b. Boxplot of time using amphetamines.';
			%else %if &var =bzd %then %let Title  = 'Figure S6c. Boxplot of time using benzodiazepines.';
			%else %if &var =any %then %let Title  = 'Figure S6d. Boxplot of time using illicit drugs.';
			%else %if &var =mtd %then %let Title  = 'Figure S6e. Boxplot of time retained in methadone.';
			%else %if &var =bup %then %let Title  = 'Boxplot of time retained in buprenorphine.';
			%else %if &var =hiv %then %let Title  =  'Figure S6f. Boxplot of time HIV positive.';

		%end;

		proc template;
			define statgraph temp;
			begingraph;
			layout overlay;
			boxplot y=&type._&var x=Arm;
			endlayout;
			endgraph;
			end;
		run;

		proc sgrender data=loc.&dat.&v template=temp;
			format arm $imp_.;
		   	label &type._&var = "Time (days)";
			label arm = "Group";
			title &title;
		run;

	%end;

	title;

%MEND;

****************************************************************************; 
/*
ods noproctitle; 

%let file = "&out_dir.\CDDC Urine Supp last obs time boxplots &date..rtf";
options nodate nonumber;
ods rtf file = &file Style = custom_narrow startpage = YES BODYTITLE;

proc template;
	define statgraph temp;
	begingraph;
	layout overlay;
	boxplot y=T_last x=arm;
	endlayout;
	endgraph;
	end;
run;

title 'Distribution of timing of last observation';
proc sgrender data=loc.&dat.&v template=temp;
	format arm $imp_.;
	label T_last = "Time (days)";
	label arm = "Group";
	
run;
title;

ods rtf close;

*/

****************************************************************************;

%let file = "&out_dir.\CDDC Urine Supp outcome free time &date..rtf";
options nodate nonumber;
ods rtf file = &file Style = custom startpage = Yes BODYTITLE;

%let type = free;
%print_free_time(&v,&type);

%let type = present;
%print_free_time(&v,&type);

%let text = 
"Note: Outcome free-time computed as time of last observation 
 multiplied by the percentage of measurements 
 which were free of the given outcome. Drug-use time and 
 retention time computed as time of last observation 
 multiplied by the percentage of measurements which were 
 positive for the given outcome.^2n";

ods text = &text;
ods text = "VTC: Voluntary Treatment Center";
ods text = "CDDC: Compulsory Drug Detention Center";
ods text = "SR: Stochastic regression imputation";
ods text = "SV90: Single value imputation of length of inpatient stay (90 days)";
ods text = "SV30: Single value imputation of length of inpatient stay (30 days)";
ods text = "SV14: Single value imputation of time of first outcome measurement (14 days)";
ods text =  "SV0: Single value imputation of time of first outcome measurement (0 days)";

ods rtf close;

****************************************************************************;
****************************************************************************;
*****************       Plot Kaplan Meier Curves          ******************;
****************************************************************************;
****************************************************************************;

%macro KM_plots(v,var,list);

	%if &var =opi %then %let text = Figure S1q. Kaplan Meier curve for opiate-free time.;
	%else %if &var =amp %then %let text = Figure S2q. Kaplan Meier curve for amphetamine-free time.;
	%else %if &var =bzd %then %let text = Figure. Kaplan Meier curve for benzodiazepine-free time.;
	%else %if &var =any %then %let text = Figure S3q. Kaplan Meier curve for illicit-drug-free time.;
	%else %if &var =mtd %then %let text = Figure S4q. Kaplan Meier curve for methadone-free time.;
	%else %if &var =bup %then %let text = Figure. Kaplan Meier curve for buprenorphine-free time.;
	%else %if &var =hiv %then %let text = Figure S5. Kaplan Meier curve for HIV-uninfected time.;

	%let i = 1;
	
	%let vals = a b c d e f g;
	
	%do %until ("%scan(&list, &i)" = "");

		%let type = %scan(&list, &i);

		%let val = %scan(&vals, &i);

		%let text1 = %sysfunc(translate(&text,&val,q));

		%if &type =A %then %let text2 = (Missing measurements are ignored);
	 	%else %if &type =B %then %let text2 = (Missing measurements coded as an event);
	 	%else %if &type =C %then %let text2 = (Missing measurements coded as a non-event);
	 	%else %if &type =D %then %let text2 = D- Worst case scenario;
	 	%else %if &type =E %then %let text2 = E- Missing equivalent to censored;
	 	%else %if &type =F %then %let text2 = F- Intervening missing a non-event/trailing missing an event;
	 	%else %if &type =G %then %let text2 = G- Reverse event coding (missing is ignored);
		%else %if &type =H %then %let text2 = (Missing measurements coded with differential risk of event);

		title "&text1";
		title2 "&text2";

		ods select SurvivalPlot;
		proc lifetest data = loc.&dat.&v
		plots=survival(nocensor atrisk(outside)=0 30 90 180 270 360) atrisk maxtime=380;
			strata arm /DIFF=ALL;
			time T&type._&var*Y&type._&var(0);
			format arm $imp_.;
			label T&type._&var = "Time since release/discharge (days)";
			label arm = "Group";
			ods output SurvDiff=temp; 
			run;
		quit;
		ods select all;

		proc print data = temp noobs label;
			title "Between Strata Log-Rank Test";
			where Test ="Logrank";
			var Stratum1 Stratum2 tukey;
			label Stratum1="Stratum 1";
			label Stratum2="Stratum 2";
			label tukey="P-value (Tukey)";
		run;

		%let i = %eval(&i + 1);

	%end;

%if "&var" ne "mtd" and "&var" ne "hiv"  %then %do;

%let text = 
Note: Unadjusted Kaplan Meier plots reflecting alternative event 
coding scenarios. For differential risk of event scenario, CDDC participants had 
a 25% chance of an event for each planned, but missed measurement. 
VTC participants had a 75% chance of an event for each planned, 
but missed measurement. Numbers at risk at 30, 90, 180, 270, 
and 365 days are provided in the table below each plot. 
See methods of manuscript and supplemental methods for 
further detail on event coding for each scenario.^2n;

	ods text = "&text";
	ods text = "VTC: Voluntary Treatment Center";
	ods text = "CDDC: Compulsory Drug Detention Center";
	ods text = "SR: Stochastic regression imputation";
	ods text = "SV90: Single value imputation of length of inpatient stay (90 days)";
	ods text = "SV30: Single value imputation of length of inpatient stay (30 days)";
	ods text = "SV14: Single value imputation of time of first outcome measurement (14 days)";
	ods text =  "SV0: Single value imputation of time of first outcome measurement (0 days)";

%end;
	title;
	title2;

%mend;

****************************************************************************;
***************         Opiate Use - Figure 2             ******************;
****************************************************************************;

%ProvideSurvivalMacros

%macro StmtsTop; %mend;

%let xOptions = labelattrs=(size=12pt)
                tickvalueattrs=(size=12pt)
				linearopts=(viewmax=400 tickvaluelist=(0 30 90 180 270 365));
%let AtRiskOpts = display=(label) valueattrs=(size=12pt) 
				HEADERlabel="                                      Numbers at risk"
				HEADERLABELATTRS=(FAMILY="Times New Roman" SIZE=12pt weight=bold);
%let yOptions = label="Probability of no opiate use" 
				labelattrs=(size=12pt)
                tickvalueattrs=(size=12pt)
				linearopts=(viewmin=0 viewmax=1
				tickvaluelist=(0 .25 .5 .75 1));
%let StepOpts = lineattrs=(thickness=0.5); 
%let legendopts = title=GROUPNAME across=1 location=inside autoalign=(TopRight);
%let InsetOpts  = location =inside AUTOALIGN=(BOTTOMLEFT)
                  border=false BackgroundColor=GraphWalls:Color Opaque=true;
%let ClassOpts = class=CLASSATRISK;

%macro pValue;
   if (PVALUE < .0001)
      entry "Log Rank p "   eval (PUT(PVALUE, PVALUE6.4));
   else
      entry "Log Rank p = " eval (PUT(PVALUE, PVALUE6.4));
   endif;
%mend;

%CompileSurvivalTemplates

proc format;
	value $group_ 	'c1'="VTC"
					'p1'='CDDC';
run;

options papersize=("8in", "6in")
	orientation=landscape
	topmargin=".5in"
	bottommargin=".25in"
	leftmargin=".25in"
	rightmargin=".25in"
	nodate nonumber;


%let var = opi;
%let type = A;
%let file = "&out_dir.\CDDC Urine Figure 2 &date..pdf";
ods noproctitle escapechar='^'; 
ods pdf file = &file style=custom_narrow_pdf;

ods select SurvivalPlot;
proc lifetest data = loc.&dat.&v
	plots=survival(nocensor test atrisk(outside)=0 30 90 180 270 365) maxtime=380;
	where arm in("c1","p1");
	strata arm;
	time T&type._&var*Y&type._&var(0);
	format arm $group_.;
	label arm = "Group";
	label T&type._&var = "Time since release/discharge (days)";
	title "Figure 2. Unadjusted probability of no opiate use, by group.";
run;
quit;
title;

ods pdf close;

goptions reset=ALL;

****************************************************************************;
**********     Reset setting for supplemental KM plots    ******************;
****************************************************************************;

%ProvideSurvivalMacros

/*
%macro StmtsTop;
	referenceline x=30;
	referenceline x=90;
	referenceline x=180;
	referenceline x=270;
	referenceline x=365;
%mend;
*/
%macro StmtsTop;
%mend;

%let xOptions = labelattrs=(size=10pt)
                tickvalueattrs=(size=10pt)
				linearopts=(viewmax=400 tickvaluelist=(0 30 90 180 270 365));
%let AtRiskOpts = display=(label) valueattrs=(size=10pt) 
				HEADERlabel="                                                             Numbers at risk"
				HEADERLABELATTRS=(FAMILY="Times New Roman" SIZE=10pt weight=bold);
%let yOptions = label="Probability of no opiate use" 
				labelattrs=(size=10pt)
                tickvalueattrs=(size=10pt)
				linearopts=(viewmin=0 viewmax=1
				tickvaluelist=(0 .25 .5 .75 1));
%let StepOpts = lineattrs=(thickness=0.5);
%let legendopts = title=GROUPNAME across=3 location=outside ;
%let InsetOpts  = location = inside AUTOALIGN=(BOTTOMLEFT)
                  border=false BackgroundColor=GraphWalls:Color Opaque=true;
%let ClassOpts = class=CLASSATRISK;

%macro pValue;
   if (PVALUE < .0001)
      entry "Log Rank p "   eval (PUT(PVALUE, PVALUE6.4));
   else
      entry "Log Rank p = " eval (PUT(PVALUE, PVALUE6.4));
   endif;
%mend;

%CompileSurvivalTemplates

ods graphics on / width=5.6in height=5.6in border=off;

options papersize=Letter
	orientation=portrait
	nodate nonumber;
ods noproctitle escapechar='^'; 

************************  Alternative Opiate Scenarios *********************;

%let var = opi;
%let list = A B C H;
%let file = "&out_dir.\CDDC Urine Supp KM Plots &var &date..rtf";
ods rtf file = &file Style = custom_narrow startpage = Yes BODYTITLE;
%km_plots(&v,&var,&list);
ods rtf close;

****************************************************************************;
*********************             Amp Use                 ******************;
****************************************************************************;

%let yOptions = label="Probability of no amphetamine use"
				labelattrs=(size=10pt)
                tickvalueattrs=(size=10pt)
				linearopts=(viewmin=0 viewmax=1
				tickvaluelist=(0 .25 .5 .75 1));
%CompileSurvivalTemplates

%let var = amp;
%let list = A B C H;
%let file = "&out_dir.\CDDC Urine Supp KM Plots &var &date..rtf";
ods rtf file = &file Style = custom_narrow startpage = Yes BODYTITLE;
%km_plots(&v,&var,&list);
ods rtf close;

****************************************************************************;
*********************             BZD Use                 ******************;
****************************************************************************;
/*
%let yOptions = label="Probability of no benzodiazepine use"
				labelattrs=(size=10pt)
                tickvalueattrs=(size=10pt)
				linearopts=(viewmin=0 viewmax=1
				tickvaluelist=(0 .25 .5 .75 1));
%CompileSurvivalTemplates

%let var = bzd;
%let list = A B C H;
%let file = "&out_dir.\CDDC Urine Supp KM Plots &var &date..rtf";
ods rtf file = &file Style = custom_narrow startpage = Yes BODYTITLE;
%km_plots(&v,&var,&list);
ods rtf close;
*/

****************************************************************************;
*********************             Any Drug Use            ******************;
****************************************************************************;
%let yOptions = label="Probability of no illict drug use"
				labelattrs=(size=10pt)
                tickvalueattrs=(size=10pt)
				linearopts=(viewmin=0 viewmax=1
				tickvaluelist=(0 .25 .5 .75 1));
%CompileSurvivalTemplates

%let var = any;
%let list = A B C H;
%let file = "&out_dir.\CDDC Urine Supp KM Plots &var &date..rtf";
ods rtf file = &file Style = custom_narrow startpage = Yes BODYTITLE;
%km_plots(&v,&var,&list);
ods rtf close;


****************************************************************************;
*********************              HIV                    ******************;
****************************************************************************;
/*
%let yOptions = label="Probability of no HIV infection"
				labelattrs=(size=10pt)
                tickvalueattrs=(size=10pt)
				linearopts=(viewmin=0 viewmax=1
				tickvaluelist=(0 .25 .5 .75 1));
%CompileSurvivalTemplates

%let var = hiv;
%let list = A;
%let file = "&out_dir.\CDDC Urine Supp KM Plots &var &date..rtf";
ods rtf file = &file Style = custom_narrow startpage = Yes BODYTITLE;
%km_plots(&v,&var,&list);

%let text = 
Note: Unadjusted Kaplan Meier plot with corresponding 
numbers at risk for 0, 30, 90, 180, 270 and 265 days in the table below each plot. ^2n;

ods text = "&text";
ods text = "VTC: Voluntary Treatment Center";
ods text = "CDDC: Compulsory Drug Detention Center";
ods text = "SR: Stochastic regression imputation";
ods text = "SV90: Single value imputation of length of inpatient stay (90 days)";
ods text = "SV30: Single value imputation of length of inpatient stay (30 days)";
ods text = "SV14: Single value imputation of time of first outcome measurement (14 days)";
ods text =  "SV0: Single value imputation of time of first outcome measurement (0 days)";
ods rtf close;
*/

****************************************************************************;
*********************             Bup Use                 ******************;
****************************************************************************;
/*
%let yOptions = label="Probability of no buprenorphine use"
				labelattrs=(size=10pt)
                tickvalueattrs=(size=10pt)
				linearopts=(viewmin=0 viewmax=1
				tickvaluelist=(0 .25 .5 .75 1));
%CompileSurvivalTemplates

%let var = bup;
%let list = A B C H;
%let file = "&out_dir.\CDDC Urine Supp KM Plots &var &date..rtf";
ods rtf file = &file Style = custom_narrow startpage = Yes BODYTITLE;
%km_plots(&v,&var,&list);
ods rtf close;
*/
****************************************************************************;
*********************             Mtd Use                 ******************;
****************************************************************************;
%let yOptions = label="Probability of no methadone linkage"
				labelattrs=(size=10pt)
                tickvalueattrs=(size=10pt)
				linearopts=(viewmin=0 viewmax=1
				tickvaluelist=(0 .25 .5 .75 1));
%CompileSurvivalTemplates

%let var = mtd;
%let list = A B C;
%let file = "&out_dir.\CDDC Urine Supp KM Plots &var &date..rtf";
ods rtf file = &file Style = custom_narrow startpage = Yes BODYTITLE;
%km_plots(&v,&var,&list);

* Add the reverse event coding plot to the methadone figures document;

%let yOptions = label="Probability of methadone retention"
				labelattrs=(size=10pt)
                tickvalueattrs=(size=10pt)
				linearopts=(viewmin=0 viewmax=1
				tickvaluelist=(0 .25 .5 .75 1));
%CompileSurvivalTemplates

%let type = G;
title "Figure S4d. Kaplan Meier curve for methadone-retention time.";
Title2 "(Missing measurements are ignored)"; 
ods select SurvivalPlot;
proc lifetest data = loc.&dat.&v
	plots=survival( atrisk(outside)=0 30 90 180 270 360) atrisk maxtime=380;
	strata arm;
	time T&type._&var*Y&type._&var(0);
	format arm $imp_.;
	label T&type._&var = "Time since release/discharge (days)";
	label arm = "Group";
run;
quit;

title;
title2;

%let text = 
Note: Unadjusted Kaplan Meier plots reflecting alternative event 
coding scenarios. For methadone-retention time, the event was defined as
the first urine measurement for which the participant 
did not test positive for methadone. Numbers at risk at 30, 90, 180, 270, 
and 365 days are provided in the table below each plot. 
See methods of manuscript for further detail on event 
coding for each scenario.^2n;

ods text = "&text";
ods text = "VTC: Voluntary Treatment Center";
ods text = "CDDC: Compulsory Drug Detention Center";
ods text = "SR: Stochastic regression imputation";
ods text = "SV90: Single value imputation of length of inpatient stay (90 days)";
ods text = "SV30: Single value imputation of length of inpatient stay (30 days)";
ods text = "SV14: Single value imputation of time of first outcome measurement (14 days)";
ods text =  "SV0: Single value imputation of time of first outcome measurement (0 days)";

ods rtf close;

****************************************************************************;

* Clean up;

proc template; 
	delete Stat.Lifetest.Graphics.ProductLimitSurvival / store=sasuser.templat;
	delete Stat.Lifetest.Graphics.ProductLimitSurvival2 / store=sasuser.templat;
run;

****************************************************************************;
****************************************************************************;
******************             KM Estimates Macro         ******************;
****************************************************************************;
****************************************************************************;

data temp_count;
	do stratum = 1 to 6;
		do T = 0 to 500;
			output;
		end;
	end;
run;


%MACRO KM_estimates(v,var,type,out,text1,text2,start);

	ods select none;
	ods output ProductLimitEstimates=temp;

	proc lifetest data = loc.&dat.&v;
		strata Arm;
		time T&type._&var*Y&type._&var(0);
		run;
	quit;
	ods select all;

	proc sort data = temp; by stratum T&type._&var; run;

	data temp2;
		merge temp temp_count (rename = (T = T&type._&var));
		by stratum T&type._&var;

		drop censor StdErr Failed Left;
	run;

	data temp3;
		set temp2 (rename = (Arm = temps3 survival = temps Failure =temps2));
		by stratum T&type._&var;
		retain Arm Survival Failure;

		if first.stratum then Arm = temps3;
		
		if temps ~= . then Survival = temps;
		if temps2 ~= . then Failure = temps2;

		if last.T&type._&var;

		drop temps temps2 temps3 stratum;

	run;

	proc sort data = temp3; by T&type._&var arm; run;

	data temp4;
		length Time 8.;
		set temp3;
		by T&type._&var Arm;
		retain v1-v6;

		if Arm = 'c1' then do
			v1=.;
			v2=.;
			v3=.;
			v4=.;
			v5=.;
			v6=.;
		end;

		if T&type._&var = 30 then do;
			if Arm = 'c1' then v1 = &out;
			else if Arm = 'c2' then v2 = &out;
			else if Arm = 'c3' then v3 = &out;
			else if Arm = 'p1' then v4 = &out;
			else if Arm = 'p2' then v5 = &out;
			else if Arm = 'p3' then v6 = &out;
			Time = 1;
		end;

		if T&type._&var = 90 then do;
			if Arm = 'c1' then v1 = &out;
			else if Arm = 'c2' then v2 = &out;
			else if Arm = 'c3' then v3 = &out;
			else if Arm = 'p1' then v4 = &out;
			else if Arm = 'p2' then v5 = &out;
			else if Arm = 'p3' then v6 = &out;
			Time = 3;
		end;

		if T&type._&var = 180 then do;
			if Arm = 'c1' then v1 = &out;
			else if Arm = 'c2' then v2 = &out;
			else if Arm = 'c3' then v3 = &out;
			else if Arm = 'p1' then v4 = &out;
			else if Arm = 'p2' then v5 = &out;
			else if Arm = 'p3' then v6 = &out;
			Time = 6;
		end;

		if T&type._&var = 270 then do;
			if Arm = 'c1' then v1 = &out;
			else if Arm = 'c2' then v2 = &out;
			else if Arm = 'c3' then v3 = &out;
			else if Arm = 'p1' then v4 = &out;
			else if Arm = 'p2' then v5 = &out;
			else if Arm = 'p3' then v6 = &out;
			Time = 9;
		end;

		if T&type._&var = 365 then do;
			if Arm = 'c1' then v1 = &out;
			else if Arm = 'c2' then v2 = &out;
			else if Arm = 'c3' then v3 = &out;
			else if Arm = 'p1' then v4 = &out;
			else if Arm = 'p2' then v5 = &out;
			else if Arm = 'p3' then v6 = &out;
			Time = 12;

		end;

		label v1 ='VTC (SR)';
		label v2 ='VTC (SV90)';
		label v3 ='VTC (SV30)';
		label v4 ='CDDC (SR)';
		label v5 ='CDDC (SV0)';
		label v6 ='CDDC (SV14)';

		if Time and Arm = 'p3';

		drop survival failure arm;

	run;

	ods startpage = &start;

	title "&text1";
	%if "&text2" ne "" %then title2 "&text2";;

	proc print data = temp4 label noobs; 
		format v1-v6 4.2;
		label T&type._&var = "Days from release/discharge";
		label Time = "Month";
		var Time T&type._&var /style(header data)={just=l};
		var v1-v6 /style(header data)={just=c};
		format T&type._&var 3.;
	
	run;
	title; title2;

	proc datasets library = work noprint;
		delete temp temp2 temp3 temp4;
	run;
	quit;


%MEND;

****************************************************************************;

* Master KM Estimates Macro;

%MACRO master_KM_estimates(v,var,out,list,text);

	%let i = 1;

	%let vals = a b c d e f g;
	
	%do %until ("%scan(&list, &i)" = "");

		%let type = %scan(&list, &i);

		%let val = %scan(&vals, &i);

		%let text1 = %sysfunc(translate(&text,&val,q));

		%if &type =A %then %let text2 = (Missing measurements are ignored);
	 	%else %if &type =B %then %let text2 = (Missing measurements coded as an event);
	 	%else %if &type =C %then %let text2 = (Missing measurements coded as a non-event);
	 	%else %if &type =D %then %let text2 = D- Worst case scenario;
	 	%else %if &type =E %then %let text2 = E- Missing equivalent to censored;
	 	%else %if &type =F %then %let text2 = F- Intervening missing a non-event/trailing missing an event;
	 	%else %if &type =G %then %let text2 = G- Reverse event coding (missing is ignored);
		%else %if &type =H %then %let text2 = (Missing measurements coded with differential risk of event);

		%if &i = 4 %then %let start = NOW; 
		%else %let start = NO;

		%KM_estimates(&v,&var,&type,&out,&text1,&text2,&start);

		%let i = %eval(&i + 1);

	%end;

%if "&var" ne "mtd" and "&var" ne "hiv"  %then %do;

%let text = 
Note: Unadjusted Kaplan Meier estimates reflecting the cumulative probability 
of no event for selected follow-up times and alternative event coding scenarios. 
For differential risk of event scenario, CDDC participants had 
a 25% chance of an event for each planned, but missed measurement. 
VTC participants had a 75% chance of an event for each planned, 
but missed measurement. See methods of manuscript for 
further detail on event coding for each scenario.^2n;

ods text = "^S={LEFTMARGIN = 1in RIGHTMARGIN = 1in}&text";
ods text = "^S={LEFTMARGIN = 1in RIGHTMARGIN = 1in}VTC: Voluntary Treatment Center";
ods text = "^S={LEFTMARGIN = 1in RIGHTMARGIN = 1in}CDDC: Compulsory Drug Detention Center";
ods text = "^S={LEFTMARGIN = 1in RIGHTMARGIN = 1in}SR: Stochastic regression imputation";
ods text = "^S={LEFTMARGIN = 1in RIGHTMARGIN = 1in}SV90: Single value imputation of length of inpatient stay (90 days)";
ods text = "^S={LEFTMARGIN = 1in RIGHTMARGIN = 1in}SV30: Single value imputation of length of inpatient stay (30 days)";
ods text = "^S={LEFTMARGIN = 1in RIGHTMARGIN = 1in}SV14: Single value imputation of time of first outcome measurement (14 days)";
ods text = "^S={LEFTMARGIN = 1in RIGHTMARGIN = 1in}SV0: Single value imputation of time of first outcome measurement (0 days)";
%end;

%MEND;

%MACRO table2_prep(var,type,out);

ods select none;
ods output ProductLimitEstimates=temp;

proc lifetest data = loc.&dat.&v;
	strata Arm;
	time T&type._&var*Y&type._&var(0);
	run;
quit;
ods select all;

proc sort data = temp; by stratum T&type._&var; run;

data temp2;
	merge temp temp_count (rename = (T = T&type._&var));
	by stratum T&type._&var;
	drop censor StdErr Failed Left;
run;

data temp3;
	set temp2 (rename = (Arm = temps3 survival = temps Failure =temps2));
	by stratum T&type._&var;
	retain Arm Survival Failure;

	if first.stratum then Arm = temps3;
	if temps ~= . then Survival = temps;
	if temps2 ~= . then Failure = temps2;

	if last.T&type._&var;

	drop temps temps2 temps3 stratum;

run;

proc sort data = temp3; by T&type._&var arm; run;

data temp_&var;
	length Time 8.;
	set temp3;
	by T&type._&var Arm;
	retain v1-v2;

	Time2 = T&type._&var;

	if Arm = 'c1' then do
		v1=.;
		v2=.;
	end;

	if T&type._&var = 30 then do;
		if Arm = 'c1' then v1 = &out;
		else if Arm = 'p1' then v2 = &out;
		Time = 1;
	end;

	if T&type._&var = 90 then do;
		if Arm = 'c1' then v1 = &out;
		else if Arm = 'p1' then v2 = &out;
		Time = 3;
	end;

	if T&type._&var = 180 then do;
		if Arm = 'c1' then v1 = &out;
		else if Arm = 'p1' then v2 = &out;
		Time = 6;
	end;

	if T&type._&var = 270 then do;
		if Arm = 'c1' then v1 = &out;
		else if Arm = 'p1' then v2 = &out;
		Time = 9;
	end;

	if T&type._&var = 365 then do;
		if Arm = 'c1' then v1 = &out;
		else if Arm = 'p1' then v2 = &out;
		Time = 12;
	end;

	if Time and Arm = 'p3';

	vtc_&var = v1;
	cddc_&var = v2;

	label vtc_&var ='VTC';
	label cddc_&var ='CDDC';

	drop survival failure arm T&type._&var v1-v2;

run;

%MEND;


****************************************************************************;
****************************************************************************;
*****     KM-estimated non-relapse/relapse rates at selected times    ******;
****************************************************************************;
****************************************************************************;

%let out =Survival;

****************************************************************************;
*********************               Table 2               ******************;
****************************************************************************;

%let type = A;

%let var = opi;
%table2_prep(&var,&type,&out);

%let var = any;
%table2_prep(&var,&type,&out);

data temp_all;
	merge temp_opi temp_any;
	by Time Time2;
run;

data restructure (keep= Time Time2 group v1 v2);
     set temp_all;
     Group=1; v1=vtc_opi; v2=cddc_opi; output;
     Group=2; v1=vtc_any; v2=cddc_any; output;
run;

proc format;

value group_ 	1 = "No opiate use"
				2 = "No illicit drug use";
run;

%let file = "&out_dir.\CDDC Urine Table 2 &date..rtf";
ods rtf file = &file Style = custom_narrow startpage = no BODYTITLE;

title "Table 2. Unadjusted probability of no drug relapse.";

proc tabulate data=restructure;
	class Time Time2 group;
	var v1 v2;
	table 	Time * Time2,
			group=' ' *(v1 v2)*max='' *f=4.2;
	label Time = "Month";
	label Time2 = "Days from release/discharge";
	format group group_.;
	label v1 = "VTC";
	label v2 = "CDDC";
run;

title;

%let text =
Note: Unadjusted Kaplan Meier estimates reflecting the cumulative probability 
of no drug relapse for selected follow-up times.^2n;

ods text = "^S={LEFTMARGIN = 1in RIGHTMARGIN = 1in}&text";
ods text = "^S={LEFTMARGIN = 1in RIGHTMARGIN = 1in}VTC: Voluntary Treatment Center";
ods text = "^S={LEFTMARGIN = 1in RIGHTMARGIN = 1in}CDDC: Compulsory Drug Detention Center";
ods text = "^S={LEFTMARGIN = 1in RIGHTMARGIN = 1in}SR: Stochastic regression imputation";
ods text = "^S={LEFTMARGIN = 1in RIGHTMARGIN = 1in}SV90: Single value imputation of length of inpatient stay (90 days)";
ods text = "^S={LEFTMARGIN = 1in RIGHTMARGIN = 1in}SV30: Single value imputation of length of inpatient stay (30 days)";
ods text = "^S={LEFTMARGIN = 1in RIGHTMARGIN = 1in}SV14: Single value imputation of time of first outcome measurement (14 days)";
ods text = "^S={LEFTMARGIN = 1in RIGHTMARGIN = 1in}SV0: Single value imputation of time of first outcome measurement (0 days)";

ods rtf close;

/*
proc print data = temp_all label noobs; 
	format vtc_opi cddc_opi vtc_any cddc_any 4.2;
	label Time2 = "Days from release/discharge";
	label Time = "Month";
	var Time Time2/style(header data)={just=l};
	var vtc_opi cddc_opi vtc_any cddc_any /style(header data)={just=c};
	format Time2 3.;
run;
title;
*/

proc datasets library = work noprint;
	delete temp temp2 temp3 temp_opi temp_any temp_all;
run;
quit;

****************************************************************************;
*********************             Opiate Use              ******************;
****************************************************************************;

%let var = opi;
%let list = A B C H;
%let file = "&out_dir.\CDDC Urine Supp KM EST opi &date..rtf";
ods rtf file = &file Style = custom_narrow startpage = no BODYTITLE;

%let text = Table S14q. Unadjusted probability of no opiate relapse.;

%master_KM_estimates(&v,&var,&out,&list,&text);

ods rtf close;

****************************************************************************;
*********************         Amphetamine Use             ******************;
****************************************************************************;

%let var = amp;
%let list = A B C H;
%let file = "&out_dir.\CDDC Urine Supp KM EST amp &date..rtf";
ods rtf file = &file Style = custom_narrow startpage = no BODYTITLE;

%let text = Table S15q. Unadjusted probability of no amphetamine relapse.;

%master_KM_estimates(&v,&var,&out,&list,&text);

ods rtf close;

****************************************************************************;
*********************           Benzo Use                 ******************;
****************************************************************************;

%let var = bzd;
%let list = A B C H;
%let file = "&out_dir.\CDDC Urine Supp KM EST bzd &date..rtf";
ods rtf file = &file Style = custom_narrow startpage = no BODYTITLE;

%let text = Table. Unadjusted probability of no benzodiazepine relapse.;

%master_KM_estimates(&v,&var,&out,&list,&text);

ods rtf close;

****************************************************************************;
*********************           Any Drug Use              ******************;
****************************************************************************;

%let var = any;
%let list = A B C H;
%let file = "&out_dir.\CDDC Urine Supp KM EST any illicit &date..rtf";
ods rtf file = &file Style = custom_narrow startpage = no BODYTITLE;

%let text = Table S16q. Unadjusted probability of no drug relapse.;

%master_KM_estimates(&v,&var,&out,&list,&text);

ods rtf close;

****************************************************************************;
******************         Buprenorphine Use              ******************;
****************************************************************************;

%let var = bup;
%let list = A B C H;
%let file = "&out_dir.\CDDC Urine Supp KM EST bup &date..rtf";
ods rtf file = &file Style = custom_narrow startpage = no BODYTITLE;

%let text = Table. Unadjusted probability of no buprenorphine use.;

%master_KM_estimates(&v,&var,&out,&list,&text);

ods rtf close;

****************************************************************************;
*********************             HIV                     ******************;
****************************************************************************;
/*
%let var = hiv;
%let list = A;
%let file = "&out_dir.\CDDC Urine Supp KM EST hiv &date..rtf";
ods rtf file = &file Style = custom_narrow startpage = no BODYTITLE;

%let text = Table S18. Unadjusted probability of no HIV infection.;

%master_KM_estimates(&v,&var,&out,&list,&text);

ods rtf close;*/

****************************************************************************;
*********************          Methadone Use              ******************;
****************************************************************************;

%let var = mtd;
%let list = A B C;
%let file = "&out_dir.\CDDC Urine Supp KM EST mtd &date..rtf";
ods rtf file = &file Style = custom_narrow startpage = no BODYTITLE;

%let text = Table S17q. Unadjusted probability of no methadone linkage.;

%master_KM_estimates(&v,&var,&out,&list,&text);

%let text1 = Table S17d. Unadjusted probability of methadone-retention.;
%let text2 = (Missing measurements are ignored); 
%let type = G;
%let start = NOW;

%KM_estimates(&v,&var,&type,&out,&text1,&text2,&start);

%let text = 
Note: Unadjusted Kaplan Meier estimates reflecting the cumulative probability 
of no linkage to methadone for selected follow-up times and alternative event coding scenarios. 
For methadone-retention scenario, estimates reflect the cumulative probability 
of methadone-retention. See methods of manuscript and supplemental methods for 
further detail on event coding for each scenario.^2n;

ods text = "^S={LEFTMARGIN = 1in RIGHTMARGIN = 1in}&text";
ods text = "^S={LEFTMARGIN = 1in RIGHTMARGIN = 1in}VTC: Voluntary Treatment Center";
ods text = "^S={LEFTMARGIN = 1in RIGHTMARGIN = 1in}CDDC: Compulsory Drug Detention Center";
ods text = "^S={LEFTMARGIN = 1in RIGHTMARGIN = 1in}SR: Stochastic regression imputation";
ods text = "^S={LEFTMARGIN = 1in RIGHTMARGIN = 1in}SV90: Single value imputation of length of inpatient stay (90 days)";
ods text = "^S={LEFTMARGIN = 1in RIGHTMARGIN = 1in}SV30: Single value imputation of length of inpatient stay (30 days)";
ods text = "^S={LEFTMARGIN = 1in RIGHTMARGIN = 1in}SV14: Single value imputation of time of first outcome measurement (14 days)";
ods text = "^S={LEFTMARGIN = 1in RIGHTMARGIN = 1in}SV0: Single value imputation of time of first outcome measurement (0 days)";

ods rtf close;
****************************************************************************;
****************************************************************************;
******************         Quartile Survival Times        ******************;
****************************************************************************;
****************************************************************************;

%MACRO quartile_estimates(v,var,type,text1,text2,start);

	ods select none;
	ods output quartiles=temp;
	proc lifetest data = loc.&dat.&v;
		strata Arm;
		time T&type._&var*Y&type._&var(0);
		run;
	quit;
	ods select all;

	proc sort data = temp; by percent arm; run;

	data temp2;
		set temp;
		by percent Arm;
		retain v1-v6;

		out = put(estimate,3.) || " " || compress("(" || put(LowerLimit,3.) || "-" || put(UpperLimit,3.) || ")"," ");

		if percent = 25 then do;
			if Arm = 'c1' then v1 = out;
			else if Arm = 'c2' then v2 = out;
			else if Arm = 'c3' then v3 = out;
			else if Arm = 'p1' then v4 = out;
			else if Arm = 'p2' then v5 = out;
			else if Arm = 'p3' then v6 = out;
		end;

		else if percent = 50 then do;
			if Arm = 'c1' then v1 = out;
			else if Arm = 'c2' then v2 = out;
			else if Arm = 'c3' then v3 = out;
			else if Arm = 'p1' then v4 = out;
			else if Arm = 'p2' then v5 = out;
			else if Arm = 'p3' then v6 = out;
		end;

		else if  percent = 75 then do;
			if Arm = 'c1' then v1 = out;
			else if Arm = 'c2' then v2 = out;
			else if Arm = 'c3' then v3 = out;
			else if Arm = 'p1' then v4 = out;
			else if Arm = 'p2' then v5 = out;
			else if Arm = 'p3' then v6 = out;
		end;

		label v1 ='VTC (SR)';
		label v2 ='VTC (SV90)';
		label v3 ='VTC (SV30)';
		label v4 ='CDDC (SR)';
		label v5 ='CDDC (SV14)';
		label v6 ='CDDC (SV0)';

		if Arm = 'p3';

		drop out Estimate Transform LowerLimit UpperLimit stratum arm;

	run;

	
	ods startpage = &start;

	title "&text1";
	%if "&text2" ne "" %then title2 "&text2";;

	proc print data = temp2 label noobs; 
		label percent = "Percentile";
		var percent/style(header data)={just=l};
		var v1-v6 /style(header data)={just=c};
	run;
	title; title2;


%MEND;

****************************************************************************;

%MACRO master_quartile_estimates(v,var,list,text);

	%let i = 1;

	%let vals = a b c d e f g;
	
	%do %until ("%scan(&list, &i)" = "");

		%let type = %scan(&list, &i);

		%let val = %scan(&vals, &i);

		%let text1 = %sysfunc(translate(&text,&val,$));

		%if &type =A %then %let text2 = (Missing measurements are ignored);
	 	%else %if &type =B %then %let text2 = (Missing measurements coded as an event);
	 	%else %if &type =C %then %let text2 = (Missing measurements coded as a non-event);
	 	%else %if &type =D %then %let text2 = D- Worst case scenario;
	 	%else %if &type =E %then %let text2 = E- Missing equivalent to censored;
	 	%else %if &type =F %then %let text2 = F- Intervening missing a non-event/trailing missing an event;
	 	%else %if &type =G %then %let text2 = G- Reverse event coding (missing is ignored);
		%else %if &type =H %then %let text2 = (Missing measurements coded with differential risk of event);

		%if &i = 5 %then %let start = NOW; 
			%else %let start = NO;

		%quartile_estimates(&v,&var,&type,&text1,&text2,&start);

		%let i = %eval(&i + 1);

	%end;

%if "&var" ne "mtd"  %then %do;

%let text = 
Note: Unadjusted Kaplan Meier estimates reflecting the quartile relapse times 
for alternative event coding scenarios. 
For differential risk of event scenario, CDDC participants had 
a 25% chance of an event for each planned, but missed measurement. 
VTC participants had a 75% chance of an event for each planned, 
but missed measurement. See methods of manuscript and supplemental methods for 
further detail on event coding for each scenario.^2n;

ods text = "^S={LEFTMARGIN = 1in RIGHTMARGIN = 1in}&text";
ods text = "^S={LEFTMARGIN = 1in RIGHTMARGIN = 1in}VTC: Voluntary Treatment Center";
ods text = "^S={LEFTMARGIN = 1in RIGHTMARGIN = 1in}CDDC: Compulsory Drug Detention Center";
ods text = "^S={LEFTMARGIN = 1in RIGHTMARGIN = 1in}SR: Stochastic regression imputation";
ods text = "^S={LEFTMARGIN = 1in RIGHTMARGIN = 1in}SV90: Single value imputation of length of inpatient stay (90 days)";
ods text = "^S={LEFTMARGIN = 1in RIGHTMARGIN = 1in}SV30: Single value imputation of length of inpatient stay (30 days)";
ods text = "^S={LEFTMARGIN = 1in RIGHTMARGIN = 1in}SV14: Single value imputation of time of first outcome measurement (14 days)";
ods text = "^S={LEFTMARGIN = 1in RIGHTMARGIN = 1in}SV0: Single value imputation of time of first outcome measurement (0 days)";
%end;

%MEND;

****************************************************************************;

 

****************************************************************************;
*********************             Opiate Use              ******************;
****************************************************************************;
%let var = opi;

%let file = "&out_dir.\CDDC Urine Supp quartile relapse opiates &date..rtf";
ods rtf file = &file Style = custom_narrow startpage = no BODYTITLE;
%let list = A B C H;
%let text1 = Table S18$. Unadjusted quartile opiate relapse times (95% CIs).;

%master_quartile_estimates(&v,&var,&list,&text1);

ods rtf close;

****************************************************************************;
*********************         Amphetamine Use             ******************;
****************************************************************************;

%let var = amp;

%let file = "&out_dir.\CDDC Urine Supp quartile relapse amphetamines &date..rtf";
ods rtf file = &file Style = custom_narrow startpage = no BODYTITLE;
%let list = A B C H;
%let text1 = Table S19$. Unadjusted quartile amphetamine relapse times (95% CIs).;

%master_quartile_estimates(&v,&var,&list,&text1);

ods rtf close;

****************************************************************************;
*********************           Benzo Use                 ******************;
****************************************************************************;
/*
%let var = bzd;

%let file = "&out_dir.\CDDC Urine Supp quartile relapse benzos &date..rtf";
ods rtf file = &file Style = custom_narrow startpage = no BODYTITLE;
%let list = A B C H;
%let text1 = Table S. Unadjusted quartile benzodiazepine relapse times (95% CIs).;

%master_quartile_estimates(&v,&var,&list,&text1);

ods rtf close;
*/
****************************************************************************;
*********************           Any Drug Use              ******************;
****************************************************************************;

%let var = any;

%let file = "&out_dir.\CDDC Urine Supp quartile relapse any drugs &date..rtf";
ods rtf file = &file Style = custom_narrow startpage = no BODYTITLE;
%let list = A B C H;
%let text1 = Table S20$. Unadjusted quartile any-drug relapse times (95% CIs).;

%master_quartile_estimates(&v,&var,&list,&text1);

ods rtf close;

****************************************************************************;
*********************          Methadone Use              ******************;
****************************************************************************;

%let var = mtd;

%let file = "&out_dir.\CDDC Urine Supp quartile relapse no mtd use &date..rtf";
ods rtf file = &file Style = custom_narrow startpage = no BODYTITLE;
%let list = A B C;
%let text1 = Table S21$. Unadjusted quartile methadone linkage times (95% CIs).;

%master_quartile_estimates(&v,&var,&list,&text1);

%let text1 = Table S21d. Unadjusted quartile methadone retention times.;
%let text2 = (Missing measurements are ignored); 
%let type = G;
%let start = NO;

%quartile_estimates(&v,&var,&type,&text1,&text2,&start);

%let text = 
Note: Unadjusted Kaplan Meier quartile times for linkage to and retention in methadone for alternative event coding scenarios. 
See methods of manuscript and supplemental methods for further detail on event coding for each scenario.^2n;

ods text = "^S={LEFTMARGIN = 1in RIGHTMARGIN = 1in}&text";
ods text = "^S={LEFTMARGIN = 1in RIGHTMARGIN = 1in}VTC: Voluntary Treatment Center";
ods text = "^S={LEFTMARGIN = 1in RIGHTMARGIN = 1in}CDDC: Compulsory Drug Detention Center";
ods text = "^S={LEFTMARGIN = 1in RIGHTMARGIN = 1in}SR: Stochastic regression imputation";
ods text = "^S={LEFTMARGIN = 1in RIGHTMARGIN = 1in}SV90: Single value imputation of length of inpatient stay (90 days)";
ods text = "^S={LEFTMARGIN = 1in RIGHTMARGIN = 1in}SV30: Single value imputation of length of inpatient stay (30 days)";
ods text = "^S={LEFTMARGIN = 1in RIGHTMARGIN = 1in}SV14: Single value imputation of time of first outcome measurement (14 days)";
ods text = "^S={LEFTMARGIN = 1in RIGHTMARGIN = 1in}SV0: Single value imputation of time of first outcome measurement (0 days)";

ods rtf close;


****************************************************************************;
******************         Buprenorphine Use              ******************;
****************************************************************************;
/*
%let var = bup;

%let file = "&out_dir.\CDDC Urine Supp quartile relapse no bup use &date..rtf";
ods rtf file = &file Style = custom_narrow startpage = no BODYTITLE;
%let list = A B C H;
%let text1 = Table. Unadjusted quartile buprenorphine use times.;

%master_quartile_estimates(&v,&var,&list,&text1);

ods rtf close;

*/
****************************************************************************;
*********************             HIV                     ******************;
****************************************************************************;
/*
%let var = hiv;

%let file = "&out_dir.\CDDC Urine Supp quartile relapse no hiv infection &date..rtf";
ods rtf file = &file Style = custom_narrow startpage = no BODYTITLE;
%let list = A;
%let text1 = Table S. Unadjusted quartile hiv infection times.;

%master_quartile_estimates(&v,&var,&list,&text1);

ods rtf close;

*/



****************************************************************************;
****************************************************************************;
*********************   Remove old datasets               ******************;
****************************************************************************;
****************************************************************************;


proc datasets lib=loc nolist;       
	delete &dat.28;   
run;
quit;


****************************************************************************;
****************************************************************************;
**************                END                ***************************;
****************************************************************************;
****************************************************************************;
%MEND print_events;
